package org.paul

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.AppCompatTextView
import androidx.constraintlayout.widget.ConstraintSet
import androidx.recyclerview.widget.DiffUtil.ItemCallback
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import org.paul.noconfig.vm.ConstraintSetExt
import org.paul.noconfig.vm.NoConfigViewModelActivity
import org.paul.withconfig.vm.WithConfigViewModelActivity

const val CODE = 0x0a

class HomeActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_home)
        val homeAdapter = HomeAdapter() { position ->
            startActivityForResult(Intent().apply {
                when (position) {
                    0 -> setClass(this@HomeActivity, NoConfigViewModelActivity::class.java)
                    1 -> {
                        setClass(this@HomeActivity, WithConfigViewModelActivity::class.java)
                        putExtra("config",set)
                    }
                }
            }, CODE)
        }
        findViewById<RecyclerView>(R.id.recycler).apply {
            addItemDecoration(DividerItemDecoration(context, DividerItemDecoration.VERTICAL))
            adapter = homeAdapter
        }
        homeAdapter.submitList(
            listOf(
                "重建activity实例",
                "回调onConfigChange"
            )
        )
    }

    var set: ConstraintSetExt? = null

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == CODE) {
            // means handle the contrainset

            this.set = data?.extras?.get("config") as ConstraintSetExt

        }
    }
}

class HomeAdapter(private val x: (Int) -> Unit) : ListAdapter<String, HomeVH>(
    object : ItemCallback<String>() {
        override fun areItemsTheSame(oldItem: String, newItem: String): Boolean {
            return oldItem == newItem
        }

        override fun areContentsTheSame(oldItem: String, newItem: String): Boolean {
            return oldItem == newItem
        }
    }
) {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HomeVH {

        val inflate = LayoutInflater.from(parent.context).inflate(R.layout.item_single_content, parent, false)
        return HomeVH(inflate)
    }

    override fun onBindViewHolder(holder: HomeVH, position: Int) {
        holder.bind(getItem(position), position, x)
    }

}

class HomeVH(itemView: View) : RecyclerView.ViewHolder(itemView) {
    fun bind(type: String, position: Int, x: (Int) -> Unit) {
        itemView.findViewById<AppCompatTextView>(R.id.content).text = type
        itemView.setOnClickListener {
            // 启动无配置带ViewModel 的 activity
            x(position)
        }
    }
}